<?php

namespace App\Models;

use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Eloquent\Model;
use Encore\Admin\Traits\DefaultDatetimeFormat;


// 出口商管理
class CustomsExporter extends BaseModel
{

    protected $table = 'yt_customs_exporter';

    public $timestamps = false;

    protected $fillable = [
        'name_cn',
        'name_en',
        'type',
        'addr',
    ];


    const BUYER = 1;
    const SELLER = 2;

    const TYPE = [
        self::BUYER => '买方',
        self::SELLER => '卖方',

    ];



    // 买方
    public static function getBuyerList()
    {
        return Cache::Remember('getBuyerList:' , config('constants.CACHE_EXPIRES'), function () {
            $fromList = self::where(['type' => self::BUYER])->pluck('name_cn', 'id');
            return $fromList;
        });
    }

    // 卖方
    public static function getSalesList()
    {
        return Cache::Remember('getSalesList:' , config('constants.CACHE_EXPIRES'), function () {
            $destList = self::where(['type' => self::SELLER])->pluck('name_cn', 'id');
            return $destList;
        });
    }


    //
    public static function getByName($name, $type)
    {
        return Cache::Remember('getExporterByNameType:'.$type. $name , config('constants.CACHE_EXPIRES'), function () use($name, $type) {

            return self::where(['name_cn' => $name, 'type' => $type])->first();
        });
    }


}
